﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;

public partial class Admin_EditCategory : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) PopulateControls();
    }
    private void PopulateControls()
    {
        string cid = Request.QueryString["cid"] ?? "";
        // Neu co QueryString cid ==> cap nhat Category
        if (cid != "")
        {
            // Lay category theo gia tri cid
            Category data = Category.Single(cid);
            // Khong ton tai category nay trong he thong
            // chuyen huong ve trang Category.aspx if (data == null)
            Response.Redirect("~/Admin/Category.aspx");

            // Nguoc lai thi dua du lieu vao form de tien hanh cap nhap

            // Dat ten trang web lblTitle.Text = "Edit Category";

            // Luu lai gia tri ID cua category hien dang cap nhat lblId.Text = data.CategoryID.ToString();

            // Gan cac thong tin con lai vao form txtName.Text = data.Name;
            txtDes.Text = data.Description;

        }
        else
        {

            btnReset.Visible = false;




            // Khong co QueryString cid ==> them moi category
            // Dat ten trang web lblTitle.Text = "Add Category"; 

            // Hien nut reset (xoa trang form de nhap lai)
            btnReset.Visible = true;
        }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        // Them su kien cho nut Save
        btnSave.Click += new EventHandler(btnSave_Click);
    }
    // Lay du lieu tu form de them moi/cap nhat
    private Category GetData()
    {
        Category data = null;
        if (lblId.Text != "")
            // lay thong tin cu tu Database de cap nhat
            data = Category.Single(lblId.Text);
        else


            data = new Category(); // them moi 
        // lay Name tu textbox Name data.Name = txtName.Text;
        // lay Description tu textbox Description data.Description = txtDes.Text;
        return data;
    }

    void btnSave_Click(object sender, EventArgs e)
    {
        // Neu tat ca du lieu deu duoc nhap hop le if (Page.IsValid)
        {
            bool rs = false;
            // Lay du lieu tu form
            Category data = GetData();

            // ID > 0 ==> Cap nhat va hien thong bao if (data.CategoryID > 0)
            if (data.CategoryID > 0)
            {
                rs = Category.Update(data);
                lblStatus.Text = rs ? "Update successful" : "Update failed";


            }
            else
            {

                rs = Category.Update(data);
                lblStatus.Text = rs ? "Update successful" : "Update failed";




                rs = Category.Add(data);
                lblStatus.Text = rs ? "Insert successful" : "Insert failed";
                // Neu them thanh cong thi xoa trang form de them tiep if (rs)
                ResetForm();
            }
        }
    }
    // Xoa trang form
    private void ResetForm()
    {
        txtName.Text = "";
        txtDes.Text = "";
    }




}
